from airtest.core.api import *

from wechat_test.page.base_page import BasePage
from wechat_test.page.me_page import MePage
from wechat_test.page.subscribe_page import SubscribePage
from wechat_test.page.theatre_page import TheatrePage


class MiniProgram(BasePage):

    def mepages(self):
        '''
        点击我得进入我的页面
        :return:
        '''
        sleep(2)
        self.log_info('跳转小程序主页，点击”我的“')
        self.poco(text='我的').click()

        return MePage(self.poco)

    def subscribe(self,name):
        '''
        点击热门，进入热门页面
        :return:
        '''
        sleep(1)
        try:
            self.log_info('点击【热门】文案')
            self.poco(text='热门').click()
        except:
            self.log_info('尝试坐标点击【热门】')
            self.poco(package='com.tencent.mm',name= 'android.widget.FrameLayout').click([0.5,0.36])

        # 点击剧目名进入播放页
        sleep(1)
        count = 0
        while count < 5:
            sleep(1)
            try:
                self.log_info(f'开始播放短剧：{name}')
                self.poco(text=f'{name}').click()
                self.poco(name='com.tencent.mm:id/yq', type='android.view.ViewGroup').click()
                self.log_info(f'点击播放暂停：{name}')
                # 点击返回
                self.poco(nameMatches="com.tencent.mm:id/.*")[4].click()
                sleep(1)
                self.poco(nameMatches="com.tencent.mm:id/.*")[2].click()
                break
            except:
                self.log_info(f'未找到该短剧：{name}')
            count += 1
            self.log_info(f'查找{count}次')

        sleep(1)
        self.log_info('跳转小程序主页，点击”追剧“')
        sleep(2)
        self.poco(text='追剧').click()
        return SubscribePage(self.poco)

    def theatre_page(self):
        '''
        点击剧场，进入剧场页面
        :return:
        '''
        self.log_info('跳转小程序主页，点击”剧场“')
        self.poco(name='android.widget.LinearLayout').child('com.tencent.mm:id/a0b')[1].click()
        return TheatrePage(self.poco)

    def zuiju(self):

        self.log_info('跳转小程序主页，点击”追剧“')
        self.poco(text='追剧').click()
        return SubscribePage(self.poco)

